<template>
  <div class="box">
    <div class="container" :class="{ active: isToggle }">
      <!-- 登录表单 -->
      <div class="form-box login box-card">
        <el-form @submit.prevent="login" :model="loginForm" :rules="rules" ref="loginForm">
          <h1>登录</h1>
          <el-form-item class="input-box" prop="username">
            <input v-model="loginForm.username" placeholder="账号" ></input>
          </el-form-item>
          <el-form-item class="input-box" prop="password">
            <input type="password" v-model="loginForm.password" placeholder="密码" />            
          </el-form-item>
          <div class="forgot-link">
            <a href="#">忘记密码</a>
          </div>
          <button type="submit" class="btn" @click="login" style="background-color: aquamarine;">登录</button>
        </el-form>
      </div>

      <!-- 注册表单 -->
      <div class="form-box register">
        <el-form @submit.prevent="register" :model="registerForm" :rules="rules" ref="registerForm">
          <h1>注册</h1>
          <el-form-item class="input-box" prop="name">
            <input type="text" v-model="registerForm.name" placeholder="用户名"  />
          </el-form-item>
          <el-form-item class="input-box" prop="username">
            <input type="text" v-model="registerForm.username" placeholder="账号"  />
          </el-form-item>
          <el-form-item class="input-box" prop="password">
            <input type="password" v-model="registerForm.password" placeholder="密码" />
          </el-form-item>
          <el-form-item class="input-box" prop="email">
            <input type="password" v-model="registerForm.email" placeholder="确认密码"/>
          </el-form-item>
          <button type="submit" class="btn" style="background-color: aquamarine;">注册</button>
        </el-form>
      </div>

      <div class="toggle-box">
        <div class="toggle-panel toggle-left">
            <div style=" display: flex;margin-bottom: 18px;">
                <img style="width: 50px; height: 50px" src="@/assets/logo.png" fit="fill"></img>
                <span style="height: 50px;line-height: 50px; margin-left: 8px; font-size: 22px;font-weight: 1000;">健康有道</span>
            </div>
          <button class="btn " @click="toggleForm">注册</button>
        </div>

        <div class="toggle-panel toggle-right">
            <div style=" display: flex;margin-bottom: 18px;">
                <img style="width: 50px; height: 50px" src="@/assets/logo.png" fit="fill"></img>
                <span style="height: 50px;line-height: 50px; margin-left: 8px; font-size: 22px;font-weight: 1000;">健康有道</span>
            </div>
          <button class="btn" @click="toggleForm">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useUserStore } from '@/stores'
import {postUserLogin,postUserRegister} from "@/api/user.js";
import md5 from 'js-md5';
import { useRouter } from 'vue-router';
const router = useRouter();
const isToggle = ref(false);
const loginForm = ref({
  username: "",
  password: "",
});
const registerForm = ref({
  name:"",
  username: "",
  password: "",
  email: "",
});

const rules = {  
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 1, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 1, max: 10, message: '账号必须是1-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{5,15}$/,
      message: '密码必须是5-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{5,15}$/,
      message: '密码必须是5-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
          如果值不等于密码
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()//成功
        }
      },
      trigger: 'blur'
    }
  ]
}

const toggleForm = () => {
  isToggle.value = !isToggle.value;
};
const userStore = useUserStore()  
// ====登录====
const login = () => {  
  loginForm.value.validate(
    async(valid)=>{
      //===入参===
      const { username, password } = loginForm.value;
      const hashedPwd = md5(md5(password));
      const paramDTO = { userAccount: username, userPwd: hashedPwd };      
      if (valid) {  
        try {
        const res = await postUserLogin(paramDTO)
        userStore.setToken(res.data.data.token)
        const role  = res.data.data.role;
        console.log(role,"角色");
        
        switch (role) {
          case 1:
          router.push("/admin");
            break;
          case 2:
          router.push("/user");
            break;
          default:
            console.warn("未知的角色类型:", role);
            break;
        }
        }  catch (error) {
          console.log(error,"错误");
        }         
      }
    }
  )
};
// ====注册====
const register = () => {  
  registerForm.value.validate(
    async(condition)=>{
      //===入参===
      const { name, username,password ,email} = registerForm.value;
        const hashedPwd = md5(md5(password));
        const paramDTO = { userAccount: username, userPwd: hashedPwd, userName: name };
        
      if (condition) {
        const res =await postUserRegister(paramDTO)
        console.log(res.data,"注册");
      }
    }
  )

};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.box {
  display: flex;
  justify-content: center; 
  align-items: center; 
  height: 100vh;
}
.container {
  position: relative;
  width: 850px;
  height: 550px;
  background-color: #fff;
  border-radius: 30px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.container.active .toggle-box::before {
  left: 50%;
}

.form-box {
  position: absolute;
  right: 0;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  color: #333;
  text-align: center;
  padding: 40px;
  z-index: 1;
  transition: 0.6s ease-in-out 1.2s, visibility 0 1;
}

.container.active .form-box {
  right: 50%;
}

.form-box.register {
  visibility: hidden;
}

.container.active .form-box.register {
  visibility: visible;
}

.form-box form {
  width: 100%;
}

.container h1 {
  font-size: 36px;
  margin: -10px 0;
}

.input-box {
  position: relative;
  margin: 30px 0;
}

.input-box label {
  position: absolute;
  top: 0;
}

.input-box input {
  width: 100%;
  padding: 13px;
  background-color: #eee;
  border: none;
  outline: none;
  font-size: 16px;
  border-radius: 8px;
  color: #333;
  font-weight: 500;
}

.input-box input::placeholder {
  color: #888;
  font-weight: 400;
}

.input-box i {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 20px;
  color: #888;
}

.forgot-link {
  margin: -15px 0 15px;
}

.forgot-link a {
  font-size: 14.5px;
  color: #333;
  text-decoration: none;
}

.btn {
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border: none;
  font-size: 16px;
  color: #fff;
  font-weight: 600;
  width: 160px;
  height: 46px;
  background-color: transparent;
  border: 2px solid #fff;
  box-shadow: none;
}

.container p {
  font-size: 14.5px;
  margin: 15px 0;
}

.social-icons {
  display: flex;
  justify-content: center;
}

.social-icons a {
  display: inline-flex;
  padding: 10px;
  border: 2px solid #ccc;
  border-radius: 8px;
  font-size: 24px;
  color: #333;
  text-decoration: none;
  margin: 0 8px;
}

.toggle-box {
  position: absolute;
  width: 100%;
  height: 100%;
}

.toggle-box::before {
  content: ""; /* 元素本身不会显示任何文本内容，但可能会显示背景、边框等其他样式。 */
  position: absolute;
  left: -250%;
  width: 300%;
  height: 100%;
  background-color: #809e4c;
  z-index: 2;
  border-radius: 150px;
  transition: 1.8s ease-in-out;
}

.toggle-panel {
  position: absolute;
  width: 50%;
  height: 100%;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2;
  transition: 0.6s ease-in-out;
}

.toggle-left {
  left: 0;
  transition-delay: 1.2s;
}

.container.active .toggle-left {
  left: -50%;
  transition-delay: 0.6s;
}

.toggle-right {
  right: -50%;
  transition-delay: 0.6s;
}

.container.active .toggle-right {
  right: 0;
  transition-delay: 1.2s;
}

.toggle-panel p {
  margin-bottom: 20px;
}

@media screen and (max-width: 650px) {
  .container {
    height: calc(100vh - 40px);
  }

  .form-box {
    bottom: 0;
    width: 100%;
    height: 70%;
  }

  .container.active .form-box {
    right: 0;
    bottom: 30%;
  }

  .toggle-box::before {
    left: 0;
    width: 100%;
    top: -270%;
    height: 300%;
    border-radius: 20vw;
  }

  .container.active .toggle-box::before {
    left: 0;
    top: 70%;
  }

  .toggle-panel {
    width: 100%;
    height: 30%;
  }

 .toggle-left {
    top: 0;
  }

  .container.active .toggle-left {
    left: 0;
    top: -30%;
  }

.toggle-right {
    right: 0;
    bottom: -30%;
  }

  .container.active .toggle-right {
    bottom: 0;
  }
}

@media screen and (max-width: 400px) {
  .form-box {
    padding: 20px;
  }

  .toggle-panel h1 {
    font-size: 30px;
  }
}
</style>
